/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package snAnalysis;


import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.StringTokenizer;

/**
 *
 * @author FUJISAI
 */
public class SNALib {
    public SNALib() {
        
    }
    
    /**
     *
     * @return String "windows" if the OS is Windows any version
     *          String "linux" if the OS is Linux/GNU Linux any version
     */
    public String getOSName() {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.indexOf("windows") != -1)
            return "windows";
        if (os.indexOf("linux") != -1)
            return "linux";
        return null;
    }

    public String getLocalIPAddress() {
        try {
            InetAddress addr = InetAddress.getLocalHost();

            byte[] ipAddr = addr.getAddress();
            String hostname = addr.getHostName();
            String ipAddress = addr.getHostAddress();

            return ipAddress;
        } catch (UnknownHostException e) {
        }
        return null;
    }

    public void examineDB(String localIP) {
        HashSet hs = new HashSet();
        MySQLBridge ms = new MySQLBridge();
        Filter bf = new Filter("chordBased");

        ResultSet rel = ms.getAllField("blog_list", "`friends`!=''");
//        ResultSet rel = ms.customQuery("SELECT * FROM `blog_list_COLTECH` LIMIT 3000");
//        ResultSet total = ms.customQuery("SELECT count(`blogID`) AS total FROM `blog_list_COLTECH`");
//        try {
//            int total_rec = Integer.parseInt(total.getString("total"));
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

        String temp1 = "";
        String temp2 = "";

        try {
            while (rel.next()) {
                temp1 = rel.getString("blogID");
                if (bf.getOutputIP(temp1).compareTo(localIP) == 0) {
                    hs.add(temp1);
                }
                temp2 = rel.getString("friends");
                StringTokenizer tokens = new StringTokenizer( temp2 );
                while ( tokens.hasMoreTokens() ) {
                    String temp = tokens.nextToken(";");
                    if (bf.getOutputIP(temp).compareTo(localIP) == 0) {
                        hs.add(temp);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(hs.size());
    }
}
